# -*- coding:utf-8 -*-

class TreeUtil():
    @staticmethod
    def build_tree(treeNodes):
        nodeList = []
        nodeDict = {}
        result = []

        for node in treeNodes:
            one = {
                "id": node[0],
                "pid": node[1],
                "name": node[2],
                "status": node[3],
                "code": node[4],
                "sort": node[5],
                "children": []
            }
            nodeDict[node[0]] = one
            nodeList.append(one)
            if one['pid'] == 0:
                result.append(one)

        for node in nodeList:
            if node['pid'] != 0:
                nodeDict[node['pid']]['children'].append(node)

        return result

    @staticmethod
    def build_tree_nodes(treeNodes):
        nodeDict = {}
        for node in treeNodes:
            nodeDict[node['id']] = node

        for node in treeNodes:
            if node['pid'] != 0:
                nodeDict[node['pid']]['children'].append(node)

        result = []

        for node in treeNodes:
            if node['pid'] == 0:
                result.append(node)

        return result